package com.rz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.admin.entity.SysDict;
import com.rz.admin.mapper.SysDictMapper;
import com.rz.admin.service.ISysDictService;
import com.rz.core.exception.GlobalException;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Rui.Zhou
 * @since 2018-07-12
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    public IPage<SysDict> getPage(SysDict sysDict) {
        QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
        sysDictQueryWrapper.like("dict_name",sysDict.getDictName());
        Page<SysDict> page= new Page<>(sysDict.getPageNo(),sysDict.getPageSize());
        return page(page,sysDictQueryWrapper);
    }

    public boolean saveDict(SysDict sysDict) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper();
        if(sysDict.getId()==null){
            queryWrapper.eq("dict_value",sysDict.getDictValue());
            SysDict dict = getOne(queryWrapper);
            if(dict!=null){
                throw new GlobalException("常量编码已存在");
            }
        }
        return saveOrUpdate(sysDict);
    }
}
